package com.seraph.td.socket;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SendSocket {
static	Socket socket;
static	public void sendFile(String fileName){
		OutputStream out ;
		byte[] buffer = new byte[1024];
		File file ;
		InputStream in;
		try {
			file = new File(fileName);
			in = new FileInputStream(file);
			int len = in.read(buffer);
			socket = new Socket("127.0.0.1",8199);
			out = socket.getOutputStream();
			while(len != -1){
				out.write(buffer);
				len = in.read(buffer);
			}
			out.flush();
			out.close();
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
static	public void send(String str){
		OutputStream out ;
		try {
			socket = new Socket("127.0.0.1",8199);
			out = socket.getOutputStream();
			byte[] buffer = str.getBytes();
			out.write(buffer);
			out.flush();
			out.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	static public void main(String[] args){
////		sendFile("s1.mp4");
//		createTestFile();
		send("seraph");
	}
	static private  void createTestFile(){
		final String fileFrame="sendDir/";
		int i =0;
		for(i=0;i<10;i++){
			try{
			File file = new File(fileFrame + i + ".txt");
			if(file.exists())
				file.delete();
			else
				file.createNewFile();
			OutputStream out = new FileOutputStream(file);
			out.write(("seraph\n"+i).getBytes());
			out.flush();
			out.close();
			}catch (Exception e) {
				// TODO: handle exception
			}
			
		}
	}
}
